A few interesting examples

library(tidyverse)
library(stationaRy)
library(sf)
library(tmap)
library(spData)

library(AOI)
library(climateR)

library(raster)
library(rasterVis)

library(patchwork)

Precip and temp for specified location (point)

# Get the data 

#loc2use <- 'Death Valley National Park'
loc2use <- 'Union College'

AOI = AOI::geocode(loc2use,
                   pt = TRUE)

ts  = getGridMET(AOI, param = c("tmax","tmin", "prcp"),
                 startDate = "2022-01-01", 
                 endDate = "2022-12-01")
# convert units 
ts <- ts %>% 
  mutate(tmax = ((tmax - 273.15)*(9/5) +32),
         tmin = ((tmin - 273.15)*(9/5) +32),
         prcp = prcp/25.4
         )
# Summary table

ts %>% 
  summarize(pcrp_max = max(prcp),
            temp_max = max(tmax),
            temp_min = min(tmin),
            temp_mean = mean((tmin+tmax)/2),
            temp_max_change = max(tmax - tmin)
            )
##   pcrp_max temp_max temp_min temp_mean temp_max_change
## 1  2.30315    96.35    -7.33  51.82579           45.54
# Make the figures 

fig_01 <- ts %>% 
  ggplot() +
  geom_line(aes(x = date, y = tmax), color = "red") +
  geom_line(aes(x = date, y = tmin), color = "blue") +
  
  labs(x = "", 
       y = "Temperature (F)"
       ) +
  
  geom_hline(yintercept = 32, linetype = "dashed") +
  theme_bw() 
  


fig_02 <- ts %>% 
  ggplot() +
  geom_col(aes(x = date, y = prcp), color = "black") +
    
  labs(y = "Precipitation (inches)") +
    
  theme_bw()
# Make the figures 

(fig_01 / fig_02) +
  plot_annotation(title = paste(loc2use,": Temperature and precipitation", sep = ""),
                  caption = "Data source: GridMET",
                  tag_levels = "a"
                  ) 

Obtain and analyze precipitation data for Hurricane Henry

henri = getGridMET(aoi_get(state = c("NY")), 
                  param = "prcp", 
                  startDate = "2021-08-16", endDate = "2021-08-24")
r = raster::stack(henri)
r_inches <- r/25.4

Static map

tmap_mode("plot") 

fig_map <- r_inches %>% 
  tm_shape() +
  tm_raster(style = "cont") +
  tm_shape(spData::us_states) +
  tm_borders()

fig_map


Interactive map

tmap_mode("view")

fig_map 


tmap_mode("plot")


Get the maximum precipitation amount for each day of the storm

round(raster::maxValue(r_inches),2) %>% as.data.frame()
##      .
## 1 1.37
## 2 3.79
## 3 4.24
## 4 3.63
## 5 2.03
## 6 4.92
## 7 6.57
## 8 2.97
## 9 0.45


Get climate data for specified region of the US

sf::sf_use_s2(FALSE)
## Spherical geometry (s2) switched off
temperature_US = getGridMET(aoi_get(state = "conus"), 
                  param = "tmin", 
                  startDate = "2022-12-24", endDate = "2022-12-24")
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on
temperature_US <- raster::stack(temperature_US)
temperature_US <- temperature_US - 273.15
temperature_US <- (temperature_US*9/5) + 32
tmap_mode("plot")
## tmap mode set to plotting
temperature_US %>% 
  tm_shape() +
  tm_raster(style = "cont", palette = "-RdBu", midpoint = 32) +
  tm_shape(spData::us_states) +
  tm_borders()

Map of springs in New Mexico

library(osmdata)
library(tigris)
library(sf)
library(osmplotr)
library(tmaptools)
library(OpenStreetMap)

Sys.setenv(MAPBOX_API_KEY = "pk.eyJ1Ijoic3RhaGxtIiwiYSI6ImNrZnJiMDMxbDA0aGsyenFlajhvMzZ4bXUifQ.I7l7fJBAHCQWRwyYozq4ZQ")
loc2use <- "New Mexico"
bb_values <- getbb(loc2use)

bb_values
##          min        max
## x -109.05022 -103.00223
## y   31.33221   37.00015
springs_data <- opq(bb_values) %>% 
  add_osm_feature(key = 'natural', value = 'spring') %>% 
  osmdata_sf()
loc_border <- spData::us_states %>% 
  filter(NAME == "New Mexico")
tmap_mode("view")
## tmap mode set to interactive viewing
map_springs <- 
  tm_shape(loc_border) +
  tm_borders(col = "black") +
tm_shape(springs_data$osm_points) +
  tm_dots(col = "blue") 
map_springs

Obtain streamflow data from the USGS (Colorado River at Lee’s Ferry example)

library(dataRetrieval)
library(lubridate)
df_stream_data <- readNWISdv(siteNumbers = "09380000",
                             parameterCd = c("00060"),
                             statCd = "00003") %>% 
  renameNWISColumns()

Daily flows

df_stream_data %>% 
  ggplot(aes(x = Date, y = Flow)) +
  geom_line() +
  
  theme_classic()


Annual flow statistics

table_flows <- df_stream_data %>% 
  mutate(Year = year(Date)) %>% 
  group_by(Year) %>% 
  summarize(mean_flow = mean(Flow, na.rm= T), 
            min_flow = min(Flow, na.rm = T),
            max_flow = max(Flow, na.rm = T),
            n_meas = n()) %>% 
  filter(n_meas > 350)

table_flows
## # A tibble: 101 x 5
##     Year mean_flow min_flow max_flow n_meas
##    <dbl>     <dbl>    <dbl>    <dbl>  <int>
##  1  1922    22196.     3700   116000    365
##  2  1923    23407.     4680    96200    365
##  3  1924    16099.     1000    72800    366
##  4  1925    17052.     1500    52300    365
##  5  1926    18041.     2500    84000    365
##  6  1927    24190.     2700   119000    365
##  7  1928    20265.     2700   113000    366
##  8  1929    27060.     4100   111000    365
##  9  1930    17119.     2900    71400    365
## 10  1931     8589.     1800    33500    365
## # ... with 91 more rows
fig_max <- table_flows %>% 
  ggplot(aes(x = Year)) +
  geom_line(aes(y = max_flow), size = 1, color = "blue") +
  #geom_line(aes(y = min_flow), size = 1, color = "red") +
  #geom_line(aes(y = mean_flow), size = 1, color = "black") +
  
  theme_classic()
  

fig_min <- table_flows %>% 
  ggplot(aes(x = Year)) +
  geom_line(aes(y = min_flow), size = 1, color = "red") +
  #geom_line(aes(y = mean_flow), size = 1, color = "black") +
  
  theme_classic()


fig_mean <- table_flows %>% 
  ggplot(aes(x = Year)) +
  geom_line(aes(y = mean_flow), size = 1, color = "black") +
  
  theme_classic()
fig_mean/fig_max/fig_min